<template>
  <div class="dashboard-container">
    <common-view
      :table-prop="tableProp"
      :form-rules="formRules"
      module-name="financeList"
      :search="getFinanceLists"
      :add="addFinance"
      :delete="deleteFinance"
      :update="updateFinance"
      :get-form-data="getFormData"
      :get-search-data="getSearchData"
    />

  </div>
</template>
<script>
import CommonView from '@/components/commonView/index.vue'
import { getAllUser } from '@/api/user'
import { getFinanceLists, addFinance, deleteFinance, updateFinance } from '@/api/finance'

export default {
  name: 'FinanceList',
  components: { 'common-view': CommonView },
  data() {
    return {
      getFinanceLists,
      addFinance,
      deleteFinance,
      updateFinance,
      userList: [],
      tableProp: [
        {
          prop: 'userId',
          label: '用户名称'
          // formatter: value => {
          //   const findUser = this.userList.find(item => item.id === value.userId)
          //   return findUser ? findUser.nickName : ''
          // }
        },
        {
          prop: 'orderName',
          label: '订单名称'
        }, {
          prop: 'money',
          label: '订单价格'
        },
        {
          prop: 'orderDesc',
          label: '订单描述'
        }
      ],
      formRules: {
        userId: [
          { required: true, message: '请选择用户', trigger: 'change' }
        ],
        money: [
          { required: true, message: '请输入订单价格', trigger: 'change' }
        ],
        orderName: [
          { required: true, message: '请输入订单名称', trigger: 'change' }
        ],
        orderDesc: [
          { required: true, message: '请输入订单描述', trigger: 'change' }
        ]
      }
    }
  },
  mounted() {
    this.init()
  },

  methods: {
    // 数据初始化
    init() {
      getAllUser().then((res) => {
        this.userList = res
      })
    },
    // 新增、编辑表单转换
    getFormData(formData, handle, id) {
      console.log('转换前', formData)
      const { userId, money, orderName, orderDesc } = formData
      const newFormData = { userId, money, orderName, orderDesc }
      if (handle === 'edit') {
        newFormData['id'] = id
      }
      return newFormData
    },
    // 查询条件转换
    getSearchData(formData) {
      const { nickName } = formData
      return {
        nickName
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }
}
</style>
